<?php
//  PULSAR, THE GAME
//  ================================
//  COPYRIGHT(c) 2013 BY KAMIL MRZYGŁÓD
//  CODENOVA.PL

// Get core
require_once '../Core/Core.php';

// Require necessary modules
Modules\Core\Core::RequireModule('Validation', 'Validator');

// Validate request
if(Modules\Validation\Validator::CheckIfEmptyOrNull([$_POST['Login'], $_POST['Password']])) {
    die('Error');
}
else
{
    // Attach POST vars to local variables
    $login = $_POST['Login'];
    $password = $_POST['Login'];
}

// If validation went OK, try to login user
Modules\Core\Core::LoadPropel();

$q = PulsarUsersQuery::create();
$user = $q->filterByLogin($login)->filterByPassword($password)->find();

// Only one user can be found
if($user->count() != 1) {
    die('Error');
}

$user = $user->getFirst();

// We have only one user, logon
Modules\Core\Core::LoadModel('User' . DIRECTORY_SEPARATOR . 'PulsarUsers');

$model = new \Models\User\PulsarUsers();
$model->Id = $user->getId();
$model->Login = $user->getLogin();
$model->Password = $user->getPassword();

// Save data
session_start();
$_SESSION['User'] = $model;

// Header
header('Location: http://127.0.0.1:8080/Pulsar/App/');
exit;